package com.gxa.meiye.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.gxa.meiye.admin.mapper.UserLocationMapper;
import com.gxa.meiye.admin.service.ConsumerLocationService;
import com.gxa.meiye.core.common.ResponseData;
import com.gxa.meiye.core.pojo.domain.lzj.Location;
import com.gxa.meiye.core.pojo.vo.UserLocationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class ConsumerLocationServiceImpl extends ServiceImpl<UserLocationMapper, UserLocationVO> implements ConsumerLocationService {

    @Autowired
    private UserLocationMapper userLocationMapper;
    @Override
    public ResponseData queryByPhone(String phone) {
        ResponseData rd=new ResponseData();
        List<UserLocationVO> cs = userLocationMapper.queryByPhone(phone);
        rd.setCode(200);
        rd.setMsg("查询成功");
        rd.setData(cs);
        return rd;
}

    @Override
    public ResponseData addLocation(String phone, Location location) {
        ResponseData rd=new ResponseData();
        Integer s = userLocationMapper.selectId(phone);
        location.setUserId(s);
        System.out.println("========================="+s);
        Integer i = userLocationMapper.addLocation(location);
        if (i > 0) {
            rd.setCode(200);
            rd.setMsg("添加成功");
        } else {
            rd.setCode(500);
            rd.setMsg("添加失败");
        }
        return rd;
    }

    @Override
    public ResponseData updateLocation(UserLocationVO userLocationVO) {
        ResponseData rd=new ResponseData();
        Integer result = userLocationMapper.updateLocation(userLocationVO);
        if(result > 0){
            rd.setCode(200);
            rd.setMsg("编辑成功");
        }
        return rd;

    }

    @Override
    public ResponseData delLocation(Integer[] ids) {
        ResponseData rd = new ResponseData();
        try{
            if(ids.length>0) {
                for(Integer id : ids){
                    userLocationMapper.delLocation(id);
                }
                rd.setCode(200);
                rd.setMsg("删除成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            rd.setCode(500);
            rd.setMsg("系统开小差，请联系管理员");
        }
        return rd;
    }
}
